home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-03-27 | 3.6 KB | 102 lines | [TEXT/ttxt] |
- ;; Extremely simple trace package, written in Scheme. Has several flaws.
-
- ;; Globals:
-
- (define *trace-list* #f) ;; List of names/values of procedures being traced.
- ;; Note that direct modification of this list does
- ;; NOT untrace procedures!
- (define *trace-on* #t) ;; Is tracing enabled or not?
-
- ;;; TRACE-MANY Trace all procedures whose names are listed, without showing
- ; arguments or results.
-
- (define (trace-many . symbols)
- (map (lambda (symbol) (e::trace-1 symbol #f #f)) symbols))
-
- ;;; E::TRACE-1 Trace the named procedure. Booleans tell wether to display
- ; arguments when called, and result when returning. Works by
- ; wrapping some extra stuff around the procecure that is bound
- ; to the given name, then assigning the result to that name.
- ; Saves the old procedure for possible untracing.
-
- (define (e::trace-1 symbol show-args show-result)
- (c::if (not (symbol? symbol))
- (begin (newline) (display symbol)
- (e::error "Not a symbol"))
- #f)
- (c::if (assv symbol *trace-list*)
- (untrace symbol)
- #f)
- (let ((old (e::cons-with-continuation symbol)))
- (c::if (not (procedure? old))
- (begin (newline) (display symbol)
- (e::error "Not a procedure"))
- (begin
- (set! *trace-list* (cons (cons symbol old) *trace-list*))
- (e::cons-with-continuation
- `(set! ,symbol
- ,(lambda stuff
- (let ((result #f))
- (c::if *trace-on*
- (begin (display "Enter ")
- (c::if show-args
- (display (cons symbol stuff))
- (display symbol))
- (newline))
- #f)
- (set! result (apply old stuff))
- (c::if *trace-on*
- (begin (display "Return ")
- (c::if show-result
- (begin (display result) (display " "))
- #f)
- (display "from ")
- (c::if show-args
- (display (cons symbol stuff))
- (display symbol))
- (newline))
- #f)
- result))))
- symbol))))
-
- ;;; TRACE Renaming of e::trace-1.
-
- (define trace e::trace-1)
-
- ;;; UNTRACE Undo the change to the binding, done by calling trace-1,
- ; and remove the symbol from the list of traced procedures.
-
- (define (untrace symbol)
- (let ((pair (assv symbol *trace-list*)))
- (c::if pair
- (begin
- (e::cons-with-continuation `(set! ,symbol ,(cdr pair)))
- (set! *trace-list* (e::remove pair *trace-list*))
- symbol)
- #f)))
-
- ;;; UNTRACE-ALL Untrace all symbols being traced.
-
- (define (untrace-all) (map untrace (tracing?)))
-
- ;;; TRACE-ON Enable tracing output.
-
- (define (trace-on) (set! *trace-on* #t))
-
- ;;; TRACE-OFF Disable tracing output.
-
- (define (trace-off) (set! *trace-on* #f))
-
- ;;; TRACING? Display procedures being traced.
-
- (define (tracing?) (map car *trace-list*))
-
- ;;; E::REMOVE Canonical code to remove first instance of thing from lyst.
-
- (define (e::remove thing lyst)
- (cond ((null? lyst) #f)
- ((not (pair? lyst)) (e::error "Improper list - \"e::remove\""))
- ((eqv? (car lyst) thing) (cdr lyst))
- (else
- (cons (car lyst) (e::remove thing (cdr lyst))))))
-